package com.lottery.dto;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lottery.utils.RandCodeUtil;

public class MessageTube {

	List<Message> list;

	int lenth;

	private int seek = 0;

	private String hashcode;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	// 创建单列
	// private static MessageTube instance = null;

	public String getHashCode() {
		return hashcode;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see person.test.component.IMessageTube#config()
	 */

	public MessageTube() {
		config();
	}

	// 获取实例
	// public static synchronized MessageTube getInstance() {
	// if (instance == null) {
	// instance = new MessageTube();
	// }
	// return instance;
	// }

	public int getSeek() {
		return seek;
	}

	public void config() {
		hashcode = RandCodeUtil.getOrderId();
		logger.debug("MessageTube create: {}", hashcode);

		Configuration config = new Configuration();
		list = new LinkedList<>();
		for (int i = 0; i < config.getLenth(); i++) {
			Message msg = new Message();
			msg.setIndex(i);
			msg.setTimestamp(0);
			list.add(msg);
		}

		this.lenth = config.getLenth();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see person.test.component.IMessageTube#push()
	 */
	public synchronized int push(Message messsage) {
		messsage.setIndex(seek);
		list.set(seek, messsage);
		int curseek = seek;
		if (seek == lenth - 1) {
			seek = 0;
		} else
			seek++;

		return curseek;
	}

	public boolean push(List<Message> list) {

		return true;
	}

	public List<Message> checkMessage(int index) {
		if (index <= seek) {
			return list.subList(index, seek);
		} else {
			List<Message> taillist = list.subList(index, lenth - 1);
			taillist.addAll(list.subList(0, seek));
			return taillist;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see person.test.component.IMessageTube#clear()
	 */
	public void clear() {
		list.clear();
	}

}
